package Modal;

import java.util.ArrayList;
import java.util.List;

public class Sale {
    private List<SaleLineItem> lineItems = new ArrayList<>();
    private Payment pay;
    private Boolean isComplete = false;

    public Payment getPay() {
        return pay;
    }

    public void makeLineItem(ProductDescription product, int qty) {
        if (isComplete)
            return;
        lineItems.add(new SaleLineItem(product, qty));
    }

    public List<SaleLineItem> getLineItems() {
        return lineItems;
    }

    public double getTotal() {
        double total = 0.0;
        for(SaleLineItem item:lineItems)
        {
            total += item.getSubTotal();
        }
        return total;
    }

    public void makePayment(double cash) {
        pay = new Payment(cash);
    }

    public double getBalance() {
        return pay.getCash() - getTotal();
    }

    public void finishASale() {
        Receipt receipt = new Receipt(this);
        receipt.print();
    }

    public String getSaleLineInfo() {
        StringBuilder saleInfo = new StringBuilder();
        for (SaleLineItem item: lineItems) {
            saleInfo.append("\n").append(item.toString());
        }
        return saleInfo.toString();
    }

    public void endPurchase() {
        isComplete = true;
    }
}